/**
 * 
 */
package com.bolbachchan.blog.service.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.bolbachchan.blog.dao.common.UserDetailsDAO;
import com.bolbachchan.blog.security.BlogUser;

/**
 * @author Chaitanya
 * 
 */
public class BlogUserDetailsService implements UserDetailsService {

    @Autowired
    private UserDetailsDAO userDetailsDao;

    /*
     * (non-Javadoc)
     * @see org.springframework.security.core.userdetails.UserDetailsService#
     * loadUserByUsername(java.lang.String)
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
	com.bolbachchan.blog.hibernate.domain.UserDetails userDetails = userDetailsDao
		.getUserDetailsByUsername(username);

	if (userDetails == null) {
	    throw new UsernameNotFoundException(username + " not found in the Database");
	}

	BlogUser user = buildUser(username, userDetails);
	return user;
    }

    private BlogUser buildUser(String username, com.bolbachchan.blog.hibernate.domain.UserDetails userDetails) {
	BlogUser user = new BlogUser(username, username, true, true, true, true, null);

	user.setFirstName(userDetails.getFirstName());
	user.setLastName(userDetails.getLastName());
	user.setCity(userDetails.getCity());
	user.setState(userDetails.getState());
	user.setPhoneNbr(userDetails.getPhone());

	return user;
    }
}
